package clientapp;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.JTable.*;

public class StoreGUI extends JFrame implements ActionListener
{
	// start of Menu
	private JMenuBar bar = new JMenuBar();
	
	private JMenu operationsM = new JMenu("Operations");
	
	private JMenu addM = new JMenu("Add");
	private JMenuItem addCustMI = new JMenuItem("Customer");
	private JMenuItem addMovMI = new JMenuItem("Movie");
	
	private JMenu delM = new JMenu("Delete");
	private JMenuItem delCustMI = new JMenuItem("Customer");
	private JMenuItem delMovMI = new JMenuItem("Movie");
	
	private JMenu editM = new JMenu("Edit");
	private JMenuItem editCustMI = new JMenuItem("Customer");
	
	private JMenu view = new JMenu("View");
	private JMenuItem sortMovMI = new JMenuItem("Sorted List of Movies");
	private JMenuItem sortAvailMovMI = new JMenuItem("Sorted List of Available Movies");
	private JMenuItem sortCustMI = new JMenuItem("Sorted List of Customers");
	private JMenuItem MovStartWithMI = new JMenuItem("List Movies starting with...");
	private JMenuItem sortMoviesOutMI = new JMenuItem("List Movies that are out");
	
	private JMenu transactionM = new JMenu("Transactions");
	private JMenuItem rentMovMI = new JMenuItem("Rent Movie");
	private JMenuItem returnMovMI = new JMenuItem("Return Movie");
	private JMenuItem addCreditMI = new JMenuItem("Add Credit");
	// end of Menu
	
	// start of Display
	private Object TableData[][] = new Object[20][5];
	
	private Object MovieHeads[] = new Object[5];
	private Object CustomerHeads[] = new Object[4];
	private Object MovieOutHeads[] = new Object[3];
	
	
	private JTable MyTable = new JTable();	
	
	private DefaultTableModel model = new DefaultTableModel();
	
	JScrollPane listSP = new JScrollPane();
	
	private TableColumn TableCol = new TableColumn(1, 400);


    private Client clientr  = new Client();
	// end of Display
	
	public StoreGUI()
	{
		super("Video Store Application");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(640,480);
		setLocationRelativeTo(null); // start frame in center of screen
		setResizable(true);
			
		// Start of menu editing
		addM.add(addCustMI);
		addCustMI.addActionListener(this);
		addM.add(addMovMI);
		addMovMI.addActionListener(this);
		operationsM.add(addM);
		operationsM.setMnemonic('o');
		
   		delM.add(delCustMI);
		delCustMI.addActionListener(this);
		delM.add(delMovMI);
		delMovMI.addActionListener(this);
		operationsM.add(delM);
		
		editM.add(editCustMI);
		editCustMI.addActionListener(this);
		operationsM.add(editM);
		
		bar.add(operationsM);
		
		view.add(sortMovMI);
		sortMovMI.addActionListener(this);
		view.add(sortAvailMovMI);
		sortAvailMovMI.addActionListener(this);
		view.add(sortCustMI);
		sortCustMI.addActionListener(this);
		view.add(MovStartWithMI);
		MovStartWithMI.addActionListener(this);
		view.add(sortMoviesOutMI);
		sortMoviesOutMI.addActionListener(this);
		bar.add(view);
		view.setMnemonic('v');
		
		transactionM.add(rentMovMI);
		rentMovMI.addActionListener(this);
		transactionM.add(returnMovMI);
		returnMovMI.addActionListener(this);
		transactionM.add(addCreditMI);
		addCreditMI.addActionListener(this);
		bar.add(transactionM);
		transactionM.setMnemonic('t');
		
		setJMenuBar(bar);
		// End of menu editing
		
		// start of Table editing
		model = new DefaultTableModel(TableData, MovieHeads);
		
		MyTable = new JTable(model);
		
		listSP = new JScrollPane(MyTable);
		
		MovieHeads[0] = "CODE";
		MovieHeads[1] = "TITLE";
		MovieHeads[2] = "CATEGORY";
		MovieHeads[3] = "NEW RELEASE";
		MovieHeads[4] = "AVAILABILITY";
		
		CustomerHeads[0] = "PHONE";
		CustomerHeads[1] = "SURNAME";
		CustomerHeads[2] = "NAME";
		CustomerHeads[3] = "CREDITS";
		
		MovieOutHeads[0] = "TITLE";
		MovieOutHeads[1] = "SURNAME";
		MovieOutHeads[2] = "PHONE";
		// end of Table editing 
		
		setLayout(new BorderLayout());
		
		add(new JPanel(),BorderLayout.NORTH);
		add(new JPanel(),BorderLayout.SOUTH);
		add(new JPanel(),BorderLayout.EAST);
		add(new JPanel(),BorderLayout.WEST);
		add(listSP, BorderLayout.CENTER);
		
		setVisible(true);
		listSP.setVisible(false);
		MyTable.setEnabled(true);
		MyTable.setShowHorizontalLines(true);
		MyTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        clientr.Start();
	}
	
	public void actionPerformed(ActionEvent e)
	{
		
		if (e.getSource() == addCustMI)
		{
			//setEnabled(false);
			new addCustomer(clientr);
		}
		
		else if (e.getSource() == addMovMI)
		{
			//setEnabled(false);
			new AddMovieFrame(clientr);
		}
		
		else if (e.getSource() == delCustMI)
		{
			//setEnabled(false);
			new DeleteFrame(false,clientr);
		}
		
		else if (e.getSource() == delMovMI)
		{
			//setEnabled(false);
			new DeleteFrame(true, clientr);
		}
		
		else if (e.getSource() == editCustMI)
		{
			clientr.sendQuery("EditCustomer1");//get combo box items
			if(clientr.getResult(0,0).equals("#No Records"))
				JOptionPane.showMessageDialog(null, "No customers in database");
			else
			{
				//setEnabled(false);
				new editCustomer(clientr);
			}
		}
		else if (e.getSource() == sortMovMI)
		{
			listSP.setVisible(false);
			model.setColumnIdentifiers(MovieHeads);
			model.setColumnCount(5);
			model.setRowCount(0);
			clientr.sendQuery("ViewAllMovies");
			if(!clientr.getResult()[0][0].equals("#No Records"))
			{
				listSP.setVisible(true);
				model.setColumnIdentifiers(MovieHeads);
				model.setRowCount(clientr.getResult().length);
				for (int I = 0; I < clientr.getResult().length; I++) // This loop displays the DvdList in the table
				{
					int J;
					for (J = 0; J < 3; J++)
					{
						model.setValueAt(clientr.getResult()[I][J], I,J);
					}
					model.setValueAt(getYesNoValue(clientr.getResult()[I][J]), I,J);
					model.setValueAt(getYesNoValue(clientr.getResult()[I][++J]), I,J);
				}
			}
			else
			{
				JOptionPane.showMessageDialog(null , "There currently no movies in store");
			}
		}
		else if (e.getSource() == sortAvailMovMI)
		{
			listSP.setVisible(false);
			model.setColumnCount(5);
			model.setRowCount(0);
			clientr.sendQuery("ViewAvailableMovies");
			
			if(!clientr.getResult()[0][0].equals("#No Records"))
			{
				listSP.setVisible(true);
				model.setColumnIdentifiers(MovieHeads);
				model.setRowCount(clientr.getResult().length);
				for (int I = 0; I < clientr.getResult().length; I++) // This loop displays the DvdList in the table
				{
					int J;
					for (J = 0; J < 3; J++)
					{
						model.setValueAt(clientr.getResult()[I][J], I,J);
					}
					model.setValueAt(getYesNoValue(clientr.getResult()[I][J]), I,J);
					model.setValueAt(getYesNoValue(clientr.getResult()[I][++J]), I,J);
				}
			}
			else
			{
				JOptionPane.showMessageDialog(null , "No movies are available");
			}
			
		}
		else if (e.getSource() == sortCustMI)
		{
			listSP.setVisible(false);
			model.setColumnCount(4);
			model.setRowCount(0);
			
			clientr.sendQuery("ViewCustomers");
			if(!clientr.getResult()[0][0].equals("#No Records"))
			{
				listSP.setVisible(true);
				model.setColumnIdentifiers(CustomerHeads);
				model.setRowCount(clientr.getResult().length);
				for (int I = 0; I < clientr.getResult().length; I++) // This loop displays the DvdList in the table
				{
					for (int J = 0; J < 4; J++)
					{
						model.setValueAt(clientr.getResult()[I][J], I,J);
					}			
				}
			}
			else
			{
			 	JOptionPane.showMessageDialog(null , "There are no customers");
			}
		}
		else if (e.getSource() == MovStartWithMI)
		{
			String UserText = JOptionPane.showInputDialog(null, "Display Movies that start with...", "Movie Search", JOptionPane.QUESTION_MESSAGE);
			int FoundCount = 0;
			
			
			model.setColumnCount(5);
			model.setRowCount(0);
			listSP.setVisible(false);
			
			clientr.sendQuery("ViewMoviesStartingWith" + "#" + UserText);
			
			
			if(!clientr.getResult()[0][0].equals("#No Records"))
			{
				listSP.setVisible(true);
				model.setColumnIdentifiers(MovieHeads);
				model.setRowCount(clientr.getResult().length);
				for (int I = 0; I < clientr.getResult().length; I++) // This loop displays the DvdList in the table
				{
					int J;
					for (J = 0; J < 3; J++)
					{
						model.setValueAt(clientr.getResult()[I][J], I,J);
					}
					model.setValueAt(getYesNoValue(clientr.getResult()[I][J]), I,J);
					model.setValueAt(getYesNoValue(clientr.getResult()[I][++J]), I,J);
				}		
			}
			else
			{
				JOptionPane.showMessageDialog(null , "No movies found with search criteria");
			}			
		}
		
		else if (e.getSource() == sortMoviesOutMI)
		{
			listSP.setVisible(false);
			clientr.sendQuery("ViewMoviesNotAvailable");
			if(!clientr.getResult()[0][0].equals("#No Records"))
			{
				listSP.setVisible(true);
				model.setColumnIdentifiers(MovieOutHeads);
				model.setColumnCount(3);		
				model.setRowCount(clientr.getResult().length);
				for (int I = 0; I < clientr.getResult().length; I++) // This loop displays the DvdList in the table
				{
					for (int J = 0; J < 3; J++)
					{
						model.setValueAt(clientr.getResult()[I][J], I,J);
					}			
				}
			}
			else
			{
				JOptionPane.showMessageDialog(null , "There are no movies out");
			}
		}
		
		else if (e.getSource() == rentMovMI)
		{
			clientr.sendQuery("RentMovieF2");
			if(clientr.getResult()[0][0].equals("#No Records"))
			{ //check to see if there are available movies to be rented
				JOptionPane.showMessageDialog(null, "There are no Movies available to rent");
			}
			else
			{
				clientr.sendQuery("RentMovieF1");
				if(clientr.getResult()[0][0].equals("#No Records"))
					JOptionPane.showMessageDialog(null, "There are no Customers");
				else
				{
					new rentMovie(clientr);
				}
			}	   
		}
		
		else if (e.getSource() == returnMovMI)
		{
			clientr.sendQuery("ReturnMovie1");
			if(clientr.getResult()[0][0].equals("#No Records"))
			{
				JOptionPane.showMessageDialog(null, "No movies are currently out");
			}
			else
			   new ReturnMovie(clientr);
		}
		
		else if (e.getSource() == addCreditMI)
		{
			clientr.sendQuery("AddCredit1");//get combo box items
			if(clientr.getResult(0,0).equals("#No Records"))
				JOptionPane.showMessageDialog(null, "No customers in database");
			else
			{
				//setEnabled(false);
				new AddCredit(clientr);
			}
		}
	}
	
	private String getYesNoValue(String num)
	{
		if(num.equals("1"))
			return "Yes";
		else
			return "No";
	}
}